import React from 'react';
import nanoid from 'nanoid';
export const exportTable = (table, fileName) => {
  const html = `<html><head><meta charset='utf-8' /></head><body>${table}</body></html>`;
  const aTag = document.createElement('a');
  const blob = new Blob([html], { type: 'application/vnd.ms-excel' });
  aTag.download = `${fileName ? fileName : new Date().getTime()}.xls`;
  aTag.href = URL.createObjectURL(blob);
  aTag.click();
  URL.revokeObjectURL(blob);
};

export const renderTable = (header, body) => {
  const tHeader = header.map(item => <th key={nanoid()}>{item}</th>);
  const tBody = body.map((val, index) => {
    return (
      <tr key={nanoid()}>
        <td>John</td>
        <td>Doe</td>
        <td>john@example.com</td>
      </tr>
    );
  });
  console.log(tHeader);
  return (
    <table border="1" align="center">
      <thead>
        <tr>{tHeader}</tr>
      </thead>
      <tbody>{tBody}</tbody>
    </table>
  );
};
